package maxbe.rel2xml.generator.docs;

import maxbe.rel2xml.generator.Column;
import maxbe.rel2xml.generator.Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.TransformerHandler;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public abstract class ValueCollector implements TransformerHandler {

	private HashMap<String, List<String>> valueLists;

	private List<Table> tables;
	private List<String> tablePath;
	private String currentTable;
	private String currentColumn;

	public ValueCollector() {
		tablePath = new ArrayList<String>();
		valueLists = new HashMap<String, List<String>>();
	}

	public void setTables(List<Table> tables) {
		this.tables = tables;
		for (Table table : tables) {
			for (Column column : table.getColumns()) {
				valueLists.put(table.getName() + '.' + column.getName(),
						new ArrayList<String>());
			}
		}
	}

	public List<String> getValueList(String qualColumnName, boolean remove) {
		List<String> result = valueLists.get(qualColumnName);
		if (remove) {
			valueLists.put(qualColumnName, new ArrayList<String>());
		}
		return result;
	}

	@Override
	public final void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		for (Table table : tables) {
			if (qName.equals(table.getName() + 's')) {
				currentTable = table.getName();
				tablePath.add(currentTable);
			}
		}
		currentColumn = qName;
	}

	@Override
	public final void characters(char[] ch, int start, int length)
			throws SAXException {
		List<String> values = valueLists
				.get(currentTable + '.' + currentColumn);
		values.add(String.copyValueOf(ch, start, length));
	}

	@Override
	public final void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals(currentTable + 's')) {
			int size = tablePath.size();
			tablePath.remove(size - 1);
			endSubtreeAction(currentTable);
			currentTable = (size > 1 ? tablePath.get(size - 2) : null);
		}
	}

	public void endSubtreeAction(String tableName) {
	}

	@Override
	public String getSystemId() {
		return null;
	}

	@Override
	public Transformer getTransformer() {
		return null;
	}

	@Override
	public void setResult(Result result) throws IllegalArgumentException {
	}

	@Override
	public void setSystemId(String systemID) {
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	@Override
	public void setDocumentLocator(Locator locator) {
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}

	@Override
	public void comment(char[] ch, int start, int length) throws SAXException {
	}

	@Override
	public void endCDATA() throws SAXException {
	}

	@Override
	public void endDTD() throws SAXException {
	}

	@Override
	public void endEntity(String name) throws SAXException {
	}

	@Override
	public void startCDATA() throws SAXException {
	}

	@Override
	public void startDTD(String name, String publicId, String systemId)
			throws SAXException {
	}

	@Override
	public void startEntity(String name) throws SAXException {
	}

	@Override
	public void notationDecl(String name, String publicId, String systemId)
			throws SAXException {
	}

	@Override
	public void unparsedEntityDecl(String name, String publicId,
			String systemId, String notationName) throws SAXException {
	}

}